package etnorservice.etnor_service.service;


import etnorservice.etnor_service.domain.po.AccessLog;
import etnorservice.etnor_service.domain.po.BlockedIp;
import etnorservice.etnor_service.mapper.AccessLogMapper;
import etnorservice.etnor_service.mapper.BlockedIpMapper;
import etnorservice.etnor_service.mapper.RestrictedDirectoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccessService {

    @Autowired
    private RestrictedDirectoryMapper restrictedDirectoryMapper;

    @Autowired
    private AccessLogMapper accessLogMapper;

    @Autowired
    private BlockedIpMapper blockedIpMapper;

    public boolean isRestrictedDirectory(String directoryPath) {
        return restrictedDirectoryMapper.existsByDirectoryPath(directoryPath);
    }

    public void logAccess(String ipAddress, String directoryPath) {
        AccessLog accessLog = new AccessLog();
        accessLog.setIpAddress(ipAddress);
        accessLog.setDirectoryPath(directoryPath);
        accessLogMapper.insertAccessLog(accessLog);
    }

    public boolean isIpBlocked(String ipAddress) {
        return blockedIpMapper.existsByIpAddress(ipAddress);
    }

    public void blockIp(String ipAddress) {
        BlockedIp blockedIp = new BlockedIp();
        blockedIp.setIpAddress(ipAddress);
        blockedIpMapper.insertBlockedIp(blockedIp);
    }

    public long countAccessLogsForIp(String ipAddress) {
        return accessLogMapper.countByIpAddressAndIsBlockedFalse(ipAddress);
    }
}